package com.heatup.apt.management.service.channer.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.heatup.apt.common.constants.TokenConstants;
import com.heatup.apt.common.dao.ChannerQrcodeMapper;
import com.heatup.apt.common.dao.TokenMapper;
import com.heatup.apt.management.pay.QrCodeUtil;
import com.heatup.apt.management.service.channer.ChannerQrcodeService;
import com.heatup.apt.model.api.ApiResultModel;
import com.heatup.apt.model.channer.ChannerQrcode;

@Service
public class ChannerQrcodeServiceImpl implements ChannerQrcodeService {
	private static final Logger logger = LoggerFactory.getLogger(ChannerQrcodeServiceImpl.class);
	
	@Autowired
	private ChannerQrcodeMapper channerQrcodeMapper;
	
	@Autowired
	private TokenMapper tokenMapper; 
	
	@Override
	public ChannerQrcode queryChannerQrcode(int id) {
		
		ChannerQrcode channerQrcode = channerQrcodeMapper.queryChannerQrcodeById(id);
		
		return channerQrcode;
	}

	/**
	 * 插入渠道二维码
	 * @param channerQrcode
	 * @return
	 */
	@Override
	public ChannerQrcode insert(ChannerQrcode channerQrcode) {
		channerQrcode.setUpdatedTime(new Date());
		if(channerQrcode.getId() > 0){
			int boolU = channerQrcodeMapper.updateChannerQrcode(channerQrcode);
			channerQrcode = channerQrcodeMapper.queryChannerQrcodeById(channerQrcode.getId());
		}else{
			channerQrcode.setCommision(new BigDecimal(0));
			channerQrcode.setCreatedTime(new Date());
			channerQrcode.setNumber(0);
			logger.info("====================================== insert channer QRCode start ================");
			int bool = channerQrcodeMapper.insertChannerQrcode(channerQrcode);
			logger.info("====================================== insert channer QRCode end ================");
		}
		
		return channerQrcode;
	}

	/**
	 * 分页查询渠道二维码
	 */
	@Override
	public Map<String, Object> queryChannerQrcodeList(Map<String, Object> map) {
		Map<String, Object> objectMap = new HashMap<>();
		int pageCount = 10 ;
		int pageSum = 0;
		int pageNum = (Integer)map.get("pageNum");
		objectMap.put("pageNum",pageNum>1?pageNum:1);
		objectMap.put("pageNum",pageNum);
		if( pageNum != 0){
			pageNum = pageCount * (pageNum-1);
			map.put("pageNum", pageNum);
			map.put("pageCount", pageCount);
		}else{
			map.put("pageCount", pageCount);
		}
		List<ChannerQrcode> list = channerQrcodeMapper.queryChannerQrcodeList(map);
		map.put("pageNum", null);
		List<ChannerQrcode> listAll = channerQrcodeMapper.queryChannerQrcodeList(map);
		objectMap.put("channerQrcode",list);
		objectMap.put("pageSum",listAll.size());
		objectMap.put("mobile",map.get("mobile"));
		objectMap.put("channelName",map.get("channelName"));
		objectMap.put("status",map.get("status"));
		
		return objectMap;
	}

	/**
	 * 根据渠道二维码id
	 * 获取带参数的二维码
	 */
	@Override
	public int updateQRCodeByChannerId(int id) throws Exception {
		ChannerQrcode channerQrcode = channerQrcodeMapper.queryChannerQrcodeById(id);
		String access_token = tokenMapper.selectTokenByKey(TokenConstants.ACCESS_TOKEN_KEY).getToken();
		logger.info("================================ get QRCode by channerId======================");
		Map<String,String>  map= QrCodeUtil.createdQRCode(access_token, String.valueOf(channerQrcode.getId()));
		if("".equals(map.get("ticket"))){
			throw new RuntimeException();
		}
		logger.info("================================ get QRCode by channerId= map : "+map+"====================="+"channerQrcodeId:"+channerQrcode.getId());
		channerQrcode.setTicket(map.get("ticket"));
		channerQrcode.setQrcodeUrl(map.get("url"));
		channerQrcode.setUpdatedTime(new Date());
		int boolU = channerQrcodeMapper.updateChannerQrcode(channerQrcode);
		logger.info("================================ update channer QRCode boolU :"+boolU+"======================");
		return boolU;
	}
	
	/**
	 * 修改渠道二维码
	 */
	@Override
	public int updateQRCode(ChannerQrcode channerQrcode) {
		int boolU = channerQrcodeMapper.updateChannerQrcode(channerQrcode);
		return boolU;
	}
	
	/**
	 * 保存渠道二维码:
	 *  1.当没有id的时候就是插入
	 *  2.当有id的时候就是修改
	 */
	@Override
	public ApiResultModel saveChannerQrcode(ChannerQrcode channerQrcode) throws Exception {
		ApiResultModel apiResultModel = new ApiResultModel();
		ChannerQrcode channerQrcodeR = insert(channerQrcode);
		int boolU = 0;
		if(channerQrcodeR.getTicket() == null || "".equals(channerQrcodeR.getTicket())){
			boolU = updateQRCodeByChannerId(channerQrcode.getId());
		}
		Map<String,Object> map = new HashMap<>();
		map.put("boolU", boolU);
		map.put("channerQrcodeR",channerQrcodeR);
		apiResultModel.setData(map);
		apiResultModel.setErrorCode(0);
		return apiResultModel;
	}
	
	

}
